package apache_commons_utils;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * @author: hu.shengjin
 * @date: 2019/12/5
 * @description: 集合交并差操作
 */
public class CollectionsTest {
    public static void main(String[] args) {
        Collection collA = new ArrayList();
        Collections.addAll(collA, "a", "b", "c");
        Collection collB = new ArrayList();
        Collections.addAll(collB, "d", "b", "c");

        System.out.println("collA:" + collA);
        System.out.println("collB:" + collB);

        //判空
        System.out.println("==============判空================");
        System.out.println("集合collA是否为空：" + CollectionUtils.isEmpty(collA));
        //判等
        System.out.println("==============判等================");
        System.out.println("集合collA 和 collB 是否相等：" + CollectionUtils.isEqualCollection(collA, collB));
        //交集
        System.out.println("==============交集================");
        Collection retainAll = CollectionUtils.retainAll(collA, collB);
        System.out.println("collA 和 collB 的交集是：" + retainAll);
        //差集
        System.out.println("==============差集================");
        Collection subtract = CollectionUtils.subtract(collA, collB);
        System.out.println("collA 和 collB 的差集是：" + subtract);
        //并集
        System.out.println("==============并集================");
        Collection union = CollectionUtils.union(collA, collB);
        System.out.println("collA 和 collB 的并集是：" + union);
    }
}
